// Copyright (c) 2025, 大连理工大学 (Dalian University of Technology)
//
// Licensed under the Mulan PSL v2.
// You can use this file according to the terms and conditions of the Mulan PSL v2.
// You may obtain a copy of the License at
//
//     http://license.coscl.org.cn/MulanPSL2
//
// THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
// EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. SEE THE MULAN PSL v2
// FOR MORE DETAILS.

/*
 * @Author: qiulin 1746365080@qq.com
 * @Date: 2024-11-05 16:38:14
 * @LastEditors: qiulin 1746365080@qq.com
 * @LastEditTime: 2024-12-23 14:55:57
 * @FilePath: /SGSim/Import/BDFImport/BulkData/Node/SPOINTParser.cpp
 */
#include "PointParser.h"

#include <algorithm>
#include <cstdint>

#include "Utility/Logging/SGLogger.h"

#include "DBManager/DBServiceFactory.h"
#include "DBManager/IService/Input/Node/IPointService.h"

#include "Import/BDFImport/BDFParserBase.h"
BULKDATA_ENTRY_PARSER_REG (POINT);

using namespace SG::DBManager;
using namespace SG::Import;
using namespace SG::DataStructure;

// 以下需要修改
enum PointKey : std::uint8_t
{
    PointKey_ID = 2,
    PointKey_CP = 3,  // 节点定义坐标系,默认为0
    PointKey_X1 = 4,
    PointKey_X2 = 5,
    PointKey_X3 = 6,
};

SG::DataStructure::Common::Status BDF::BULKDATA::ParsePOINT (const std::shared_ptr<DBServiceFactory>& dbServiceFactory,
                                                             std::vector<std::string>&                dividedEntryInfo)
{
    SG::DataStructure::FEM::PointData tempData;  // 用于临时存储每个约束信息

    // TODO(bug 001) 填写正确的 m_domainId，目前暂时设置为 1
    tempData.m_domainId = 1;

    int curIDKey = 1;  // 当前字符串是第几项

    for (std::string curStrKey : dividedEntryInfo)
    {
        // 检查当前字符串是否为空白
        bool const isEmpty = is_all_space (curStrKey);

        // 如果当前字符串为空白，则跳过并处理下一个
        if (isEmpty)
        {
            curIDKey++;
            continue;
        }

        switch (curIDKey)
        {
        case PointKey::PointKey_ID:
            tempData.m_id = convertToI64 (curStrKey);
            break;
        case PointKey::PointKey_CP:
            tempData.m_CP = convertToI64 (curStrKey);
            break;
        case PointKey::PointKey_X1:
            tempData.m_X[0] = convertToReal (curStrKey);
            break;
        case PointKey::PointKey_X2:
            tempData.m_X[1] = convertToReal (curStrKey);
            break;
        case PointKey::PointKey_X3:
            tempData.m_X[2] = convertToReal (curStrKey);
            break;
        default:
            break;
        }
        curIDKey++;
    }

    // 遍历保存的约束数据，并将其添加到数据库中
    if (dbServiceFactory->get<IPointService> ()->Add (tempData) == Common::Status::STATUS_Failure)
    {
        throw std::runtime_error ("Failed to add Point data to database.");
        return Common::Status::STATUS_Failure;
    }

    return Common::Status::STATUS_Success;
}